/*
 * Programmer(s): Talon; 
 * Date: Sept 6, 2008
 * Project: Chapter.10
 * Package: default
 * FileName: SmartTest.java
 * Description: A whacked out IQ Test. 
 * Version: 1.00
 */

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import javax.swing.*;

public class SmartTest extends JFrame implements ActionListener
{
	private JRadioButton	ans1;
	private JRadioButton	ans2;
	private JRadioButton	ans3;
	private JRadioButton	ans4;
	private JRadioButton	ans5;
	private ButtonGroup	 answers;
	private JTextField	 customAns;
	private JButton	     ok;
	private JTextPane	 qArea;
	private JScrollPane	 scroller;
	private String[]	 lvl1Q	= {
	        "What is the meaning of life, the universe, and everything? Mainly because I wish to know the answer. So as long as the answer makes some sense, I will know. Hurray for me.",
	        "Why are you a towel?" };
	private String[]	 lvl1Aa	= { "42", "3" };
	private String[]	 lvl1Ab	= { "63", "because towels pwn face" };
	private String[]	 lvl1Ac	= { "To do barrel rolls", "your a towel" };
	private String[]	 lvl1Ad	= { "To hail TowelZ0r, king of all towel spawn.", "why not?" };
	private String[]	 lvl1CA	= { "42", "your a towel" };
	private int	         currQ	= 0;

	public SmartTest()
	{
		initComponents();
	}

	private void initComponents()
	{
		answers = new ButtonGroup();
		scroller = new JScrollPane();
		ok = new JButton();
		qArea = new JTextPane();
		ans1 = new JRadioButton();
		ans2 = new JRadioButton();
		ans3 = new JRadioButton();
		ans4 = new JRadioButton();
		ans5 = new JRadioButton();
		customAns = new JTextField();
		scroller.setViewportView(qArea);

		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

		ok.setText("OK");
		ok.addActionListener(this);

		qArea.setEditable(false);

		answers.add(ans1);
		ans1.setText("Answer 1");
		ans1.addActionListener(this);

		answers.add(ans2);
		ans2.setText("Answer 2");
		ans2.addActionListener(this);

		answers.add(ans3);
		ans3.setText("Answer 3");
		ans3.addActionListener(this);

		answers.add(ans4);
		ans4.setText("Answer 4");
		ans4.addActionListener(this);

		answers.add(ans5);
		ans5.setText("None of the above.");
		ans5.addActionListener(this);

		GroupLayout layout = new GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(
		        layout.createSequentialGroup().addContainerGap().addComponent(scroller, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
		                GroupLayout.PREFERRED_SIZE).addGap(18, 18, 18).addGroup(
		                layout.createParallelGroup(GroupLayout.Alignment.TRAILING).addGroup(
		                        layout.createSequentialGroup().addGroup(
		                                layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(ans5, GroupLayout.Alignment.TRAILING)
		                                        .addComponent(ans4, GroupLayout.Alignment.TRAILING).addComponent(ans3, GroupLayout.Alignment.TRAILING)
		                                        .addComponent(ans2, GroupLayout.Alignment.TRAILING).addComponent(ans1, GroupLayout.Alignment.TRAILING)
		                                        .addComponent(customAns, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE))
		                                .addGap(3, 3, 3)).addComponent(ok)).addContainerGap()));
		layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(
		        layout.createSequentialGroup().addContainerGap().addGroup(
		                layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(
		                        layout.createSequentialGroup().addComponent(ans1).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(ans2)
		                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(ans3).addPreferredGap(
		                                        LayoutStyle.ComponentPlacement.RELATED).addComponent(ans4).addGap(2, 2, 2).addComponent(ans5).addPreferredGap(
		                                        LayoutStyle.ComponentPlacement.RELATED).addComponent(customAns, GroupLayout.PREFERRED_SIZE,
		                                        GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
		                                .addComponent(ok)).addComponent(scroller, GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE)).addContainerGap()));

		pack();
		setup();
	}

	public static void main(String args[])
	{
		java.awt.EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				new SmartTest().setVisible(true);
			}
		});
	}

	@SuppressWarnings("deprecation")
	private void setup()
	{
		this.setTitle("Smart Test - Java Version");
		this.setLocation(600, 400);
		customAns.setEnabled(false);
		customAns.setBackground(Color.GRAY);
		qArea.setText(lvl1Q[currQ]);
		ans1.setText(lvl1Aa[currQ]);
		ans2.setText(lvl1Ab[currQ]);
		ans3.setText(lvl1Ac[currQ]);
		ans4.setText(lvl1Ad[currQ]);

		this.pack();
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		if (ans5.isSelected())
		{
			customAns.setEnabled(true);
			customAns.setBackground(Color.WHITE);
			customAns.requestFocus();
		}
		else
		{
			customAns.setEnabled(false);
			customAns.setText("");
			customAns.setBackground(Color.GRAY);
		}

		if (e.getSource().equals(ok))
		{
			if(getSelectedJRadioButton(answers).getText().equals(lvl1CA[currQ]) || customAns.getText().equals(lvl1CA[currQ]))
			{
				try
				{
					currQ++;
					qArea.setText(lvl1Q[currQ]);
					ans1.setText(lvl1Aa[currQ]);
					ans2.setText(lvl1Ab[currQ]);
					ans3.setText(lvl1Ac[currQ]);
					ans4.setText(lvl1Ad[currQ]);
					pack();
				}
				catch(ArrayIndexOutOfBoundsException ex)
				{
					JOptionPane.showMessageDialog(null,"You have just won 'The Game'", "Congratulations", JOptionPane.INFORMATION_MESSAGE);
					System.exit(1);
				}
			}
			else
			{
				this.dispose();
				main(null);
				
			}
		}

	}

	public JRadioButton getSelectedJRadioButton(ButtonGroup bg)
	{
		JRadioButton back = null;
		JRadioButton tmp = null;
		Enumeration enumz = bg.getElements();

		while (enumz.hasMoreElements())
		{
			tmp = (JRadioButton) enumz.nextElement();
			if (tmp.isSelected())
			{
				back = tmp;
			}
		}
		return back;
	}
}



//TODO: Create Question object that gets questions and answers from a text file.
//TODO: Have text word wrap in JTextPane